package com.etrans.web.gps.business.report.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.common.MGTemplete;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;

/**
 * 系统每月通报Service
 * @author 
 * 2018/10/08
 */
@Service
public class NotificationService extends BaseService{
	
	private final static String tableName = "enterprise_swap_month"; //引用统一的参数配置类 

	/**
	 * 入网率低于99%的业户明细/上线率低于90%的业户明细/
	 * 数据合格率低于99%的业户明细/轨迹完整率低于90%的业户明细
	 * @param queryTime
	 * @param type
	 * @return
	 */
	public List<Document> loadTableData(String queryTime, int type) {
		//过滤条件
		String query_id = queryTime.replace("-", "").substring(2, 6) + "01";
		Pattern pattern = Pattern.compile("^"+query_id);
		Map<String, Object> query = new HashMap<>();
		String sortStr = "";
		query.put("_id", pattern);
		if (type == 1) {
			query.put("vehicle_access_rate", new BasicDBObject("$lt", 99));
			sortStr = "vehicle_access_rate";
		}else if(type == 2) {
			query.put("vehicle_online_rate", new BasicDBObject("$lt", 90));
			sortStr = "vehicle_online_rate";
		}else if(type == 3) {
			query.put("data_correct_rate", new BasicDBObject("$lt", 99));
			sortStr = "data_correct_rate";
		}else if(type == 4) {
			query.put("track_complete_rate", new BasicDBObject("$lt", 90));
			sortStr = "track_complete_rate";
		}
		Bson filter = new BasicDBObject(query);
		//排序
		Bson sort = new BasicDBObject(sortStr, -1);
		FindIterable<Document> result = MGTemplete.getCollection(DBNAME, tableName).find(filter).sort(sort);
		List<Document> resultList = new ArrayList<>();
		for (Document doc : result) {
			resultList.add(doc);
		}
		return resultList;
	}
	
	/**
	 * 超速报警次数最多的10家运输企业/超速报警次数最多的10辆车辆/
	 * 疲劳驾驶总时长最多的前10家运输企业/疲劳驾驶时长最多的前10辆车辆
	 * @param queryTime
	 * @param type
	 * @return
	 */
	public List<Document> loadTableDetailData(String queryTime, int type) {
		//过滤条件
		String query_id = queryTime.replace("-", "").substring(2, 6) + "01";
		Pattern pattern = Pattern.compile("^"+query_id);
		Map<String, Object> query = new HashMap<>();
		String sortStr = "";
		query.put("_id", pattern);
		if (type == 5) {
			query.put("vehicle_access_rate", new BasicDBObject("$lt", 99));
			sortStr = "vehicle_access_rate";
		}else if(type == 6) {
			query.put("vehicle_online_rate", new BasicDBObject("$lt", 90));
			sortStr = "vehicle_online_rate";
		}else if(type == 7) {
			query.put("data_correct_rate", new BasicDBObject("$lt", 99));
			sortStr = "data_correct_rate";
		}else if(type == 8) {
			query.put("track_complete_rate", new BasicDBObject("$lt", 90));
			sortStr = "track_complete_rate";
		}
		Bson filter = new BasicDBObject(query);
		//排序
		Bson sort = new BasicDBObject(sortStr, -1);
		FindIterable<Document> result = MGTemplete.getCollection(DBNAME, tableName).find(filter).sort(sort);
		List<Document> resultList = new ArrayList<>();
		for (Document doc : result) {
			resultList.add(doc);
		}
		return resultList;
	}
}
